package com.aiyige.page.my.order.sellerOrder;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.eventbus.EventOrderScreen;
import com.aiyige.model.Subject;
import com.aiyige.page.my.order.dialog.OrderClassifyBottomDialog;
import com.aiyige.page.my.order.model.ScreenEntity;
import com.aiyige.page.my.order.model.SubjectSelection;
import com.aiyige.page.my.order.model.TimeSelectionEntity;
import com.aiyige.utils.widget.MyEditText;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SlidingTabLayout;

import net.cachapa.expandablelayout.ExpandableLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xiachao on 2017/11/16.
 */
@Route(path = ARouterConfig.OrderManagerPage,extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class OrderManagerPage extends BaseActivity implements OrderClassifyBottomDialog.Listener{

    @BindView(R.id.mainVp)
    ViewPager mainVp;
    @BindView(R.id.tabLayout)
    SlidingTabLayout tabLayout;

    @BindView(R.id.et_search)
    MyEditText inputEt;

    @BindView(R.id.cancelLayout)
    ExpandableLayout cancelLayout;

    List<TimeSelectionEntity> timeList = new ArrayList<>();
    List<SubjectSelection> goodsTypeList = new ArrayList<>();
    ScreenEntity screenEntity = new ScreenEntity();

    public static void start() {
        ARouter.getInstance().build(ARouterConfig.OrderManagerPage).navigation();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_orders_manager);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        mainVp.setAdapter(new OrderPagerAdapter(getSupportFragmentManager()));
        mainVp.setOffscreenPageLimit(getTimeList().size());
        tabLayout.setViewPager(mainVp);
        inputEt.setActionSearch(new MyEditText.ActionCallback() {
            @Override
            public void actionSearch(String value) {
                SearchOrderManagerPage.start(value);
            }
        });
        inputEt.setEditTextHint(getString(R.string.search));
    }



    private void showScreenDialog() {
        OrderClassifyBottomDialog dialog = OrderClassifyBottomDialog.newInstance();
        dialog.setLisenter(this);
        dialog.setData(getClassifyList(),getTimeList());
        dialog.show(getSupportFragmentManager(),"");
    }


    @OnClick({R.id.titleBackBtn, R.id.iv_screen})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
            case R.id.iv_screen:
                showScreenDialog();
                break;
        }
    }

    private List<TimeSelectionEntity> getTimeList() {
        if (timeList.isEmpty()) {
            timeList.add(new TimeSelectionEntity(ScreenEntity.THREE_DAY,false));
            timeList.add(new TimeSelectionEntity(ScreenEntity.ONE_WEEK,false));
            timeList.add(new TimeSelectionEntity(ScreenEntity.TWO_WEEK,false));
            timeList.add(new TimeSelectionEntity(ScreenEntity.ONE_MONTH,false));
            timeList.add(new TimeSelectionEntity(ScreenEntity.TWO_MONTH,false));
        }
        return timeList;
    }

    private List<SubjectSelection> getClassifyList() {
        if (goodsTypeList.isEmpty()) {
            goodsTypeList.add(new SubjectSelection(Subject.video_course,false));
            goodsTypeList.add(new SubjectSelection(Subject.major_course,false));
//            goodsTypeList.add(new SubjectSelection(Subject.training_course,false));
//            goodsTypeList.add(new SubjectSelection(Subject.private_course,false));
//            goodsTypeList.add(new SubjectSelection(Subject.major_course,false));
        }
        return goodsTypeList;
    }

    @Override
    public void onFinish() {
        if (screenEntity.getTypes() != null) {
            screenEntity.getTypes().clear();
        }
        screenEntity.setTime("");
        for (SubjectSelection goodsType : goodsTypeList) {
            if (goodsType.isSelected()) {
                screenEntity.addType(goodsType);
            }
        }
        for (TimeSelectionEntity timeSelectionEntity : timeList) {
            if (timeSelectionEntity.isSelected()) {
                screenEntity.setTime(timeSelectionEntity.getTime());
            }
        }
        EventBus.getDefault().post(new EventOrderScreen(screenEntity));
    }


    public class OrderPagerAdapter extends FragmentPagerAdapter {
        List<OrderDataPage> pageList = new LinkedList<>();



        public OrderPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
            pageList.add(OrderDataPage.newInstance(OrderDataPage.TYPE_All_PAGE_1));
            pageList.add(OrderDataPage.newInstance(OrderDataPage.TYPE_WAITING_PAY_PAGE_2));
            pageList.add(OrderDataPage.newInstance(OrderDataPage.TYPE_WAITING_SERVICE_PAGE_3));
            pageList.add(OrderDataPage.newInstance(OrderDataPage.TYPE_WAITING_AFFIRM_PAGE_4));
            pageList.add(OrderDataPage.newInstance(OrderDataPage.TYPE_AFTER_SALE_PAGE_5));
        }

        @Override
        public Fragment getItem(int position) {
            return pageList.get(position);
        }

        @Override
        public int getCount() {
            return pageList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return pageList.get(position).getPageTitle();
        }
    }
}
